import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const actions = {
	//添加一个todo
	addTodo({commit,state},todoObj){
		//判断是否有同名的事情
		const result = state.todos.find( t => t.title === todoObj.title)
		if(result){
			alert('不可重复添加！')
		}else{
			commit('ADD_TODO',todoObj)
		}
	}
}

const mutations = {
	ADD_TODO(state,todoObj){
		state.todos.unshift(todoObj)
	}
}

const state = {
	todos:[
		{id:'g76tyds76d',title:'吃饭',done:true},
		{id:'9i8u9sadou',title:'睡觉',done:false},
		{id:'oi7y87h898',title:'学习',done:true}
	]
}

export default new Vuex.Store({
	actions,
	mutations,
	state
})